!// Dmitry.Konovalov@jcu.edu.au Dmitry.A.Konovalov@gmail.com, 2013

! Keep all temp run-time data in this module
module jm_data_mod;
use io_mod;
use grid_mod;
use memory_mod;
use jm_e1_mod;
use jm_conf_mod;
implicit none;

!integer, parameter :: JM_DATA_MAX_L_PAR = 16;

!private;
!public setup_jm_input &
!&, JM_TRGT_T, JM_GRID_T, JM_DATA_T;

integer, parameter :: JM_DATA_CALC_HY_SWAVE_JM = 10;
integer, parameter :: JM_DATA_CALC_HY_SWAVE_HY = 20;
integer, parameter :: JM_DATA_CALC_HY = 30;
integer, parameter :: JM_DATA_CALC_HE_SWAVE = 40;
integer, parameter :: JM_DATA_CALC_HE = 50;
integer, parameter :: JM_DATA_CALC_HE_ION = 60;

type JM_DATA_T
  integer :: CALC_TYPE;
  
  ! io
  integer :: INPUT_FILE_UNIT;
  
  character(LEN=64) :: OUT_DIR;
  character(LEN=16) :: MODEL_DIR;
  character(LEN=16) :: MODEL_NAME; 
  type(GRID_LCR_T) :: lcr;

  ! TARGET
  integer :: tmaxL;  ! target ONE-electron maxL
  integer, pointer :: tmaxN(:);  !  Nt(1:tmaxL+1)
  
  integer :: tmaxLL, tmaxSS;  ! target wf max L and spin*2
  integer :: tConfMaxi;
  type(CONF_T), pointer :: tConfArr(:); ! array of target electron-configs
  type(CONF_H_MTRX) :: tConfH;

  ! SYSTEM
! system = target + scattering electron
  integer :: smaxL, smaxS2;  ! system wf max L and spin*2 
  integer :: sConfMaxi;
  type(CONF_T), pointer :: sConfArr(:); ! array of system electron-configs

  ! JM-method
  integer :: jmaxL;
  integer, pointer :: jmaxN(:); !JM's N, (N=0,1,...,jmaxN)
  real(DP), pointer :: jmLmbd(:);
  
  type(ARR_VEC_PTR_T), pointer :: orthN(:); !x, n, L; a-array

  real(DP), pointer :: wfN(:,:,:); ! x, n, L
  
  ! SCATTERING
  integer :: scttNumE; ! number of energies
  real(DP) :: scttMinE, scttMaxE; ! min/max scattering energies
  real(DP), pointer :: scttEngs(:); ! scattering energies
  
end type

type(JM_DATA_T), save :: JM_DATA; ! global

contains

SUBROUTINE jm_data_setup();  
  character(LEN=64) :: name = "jm_data_setup"; 
  call dbg_in(name); 
  
  !TODO: read from file
  
  call jm_data_init(JM_DATA);
  call jm_data_hy_setup(JM_DATA);
  
  !call load_hy_swave_basis_jm();
  !call load_hy_swave_basis_hy();
  !call load_he_swave();
  
  
  call jm_data_assert_valid(JM_DATA);
  call dbg_out(name); 
end SUBROUTINE

SUBROUTINE jm_data_hy_setup(pd);  
  type(JM_DATA_T) :: pd; ! pd-pointer to data 
  character(LEN=64) :: name = "jm_data_hy_setup";
  call dbg_in(name);
  pd%CALC_TYPE = JM_DATA_CALC_HY;
  
  pd%OUT_DIR = "C:\dev\physics\papers\output";
  pd%MODEL_NAME = "hyd";
  pd%MODEL_DIR = pd%MODEL_NAME;
      
! real(DP) :: LCR1;  
! param for first lsr grid point; LCR_FIRST = X1 - 2. * Math.log(TARGET_Z); e.g. X1=-5
  pd%lcr%xmin = -D5;  
  pd%lcr%rmax = D200;  
  pd%lcr%nx = 1001;
  
  call jmdata_scatt_engs(pd, 0.01_DP, D1, 100);
  
  pd%jmaxL = 1;  ! jm max L
  pd%tmaxL = 1;  ! target ONE-electron wf max L
  
  pd%jmaxN  => mem_veci2(pd%jmaxL+1, (/ 10, 10, 13 /));  
  call veci_dbg("pd%jmaxN=", pd%jmaxN);
  pd%tmaxN  => mem_veci2(pd%jmaxL+1, (/ 9, 8, 12 /));  
  call veci_dbg("pd%tmaxN=", pd%tmaxN);
  pd%jmLmbd => mem_vec2(pd%jmaxL+1, (/ D1, D05, D1/D3 /));
  
  call dbg_out(name);
end SUBROUTINE


! sets up scattering energies
SUBROUTINE jmdata_scatt_engs(pd, minE, maxE, nE)
  type(JM_DATA_T), intent(OUT) :: pd;
  integer, intent(IN) :: nE;
  real(DP), intent(IN) :: minE, maxE;
  character(LEN=64) :: name = "jmdata_scatt_engs";
  call dbg_in(name);
  pd%scttMinE = minE;
  pd%scttMaxE = maxE;
  call dbg_assert_true("minE<maxE", minE<maxE);
  pd%scttNumE = nE; 
  pd%scttEngs => mem_vec(nE);   
  call grid_step(pd%scttEngs, minE, maxE);
  call vec_dbg("scttEngs=", pd%scttEngs);
end SUBROUTINE


SUBROUTINE jm_data_assert_valid(pd);  
  type(JM_DATA_T) :: pd; ! pd-pointer to data 
  character(LEN=64) :: name = "jm_data_assert_valid";
  call dbg_in(name);
  ! place all data checks here    
  !
end SUBROUTINE

SUBROUTINE jm_data_init(pd);  
  type(JM_DATA_T) :: pd; ! pd-pointer to data 
  character(LEN=64) :: name = "jm_data_init";
  call dbg_in(name);
  
  pd%CALC_TYPE = -1;
  pd%INPUT_FILE_UNIT = -1;
 
  write(pd%OUT_DIR, *) NAME_NOT_SET;
  write(pd%MODEL_DIR, *) NAME_NOT_SET;
  write(pd%MODEL_NAME, *) NAME_NOT_SET;
  call grid_lcr_init(pd%lcr);

  ! TARGET
  pd%tmaxL = -1;
  pd%tmaxN => NULL();
  pd%tmaxLL = -1;  pd%tmaxSS = -1;
  pd%tConfMaxi = -1;
  pd%tConfArr => NULL();
!
!  ! SYSTEM
!! system = target + scattering electron
!  integer :: smaxL, smaxS2;  ! system wf max L and spin*2 
!  integer :: sConfMaxi;
!  type(CONF_T), pointer :: sConfArr(:); ! array of system electron-configs
!
!  ! JM-method
!  integer :: jmaxL;
!  integer, pointer :: jmaxN(:); !JM's N, (N=0,1,...,jmaxN)
!  real(DP), pointer :: jmLmbd(:);
!  
!  type(ARR_VEC_PTR_T), pointer :: orthN(:); !x, n, L; a-array
!
!  real(DP), pointer :: wfN(:,:,:); ! x, n, L
!  
!  ! SCATTERING
!  integer :: scttNumE; ! number of energies
!  real(DP) :: scttMinE, scttMaxE; ! min/max scattering energies
!  real(DP), pointer :: scttEngs(:); ! scattering energies

 
  
end SUBROUTINE


end module